//
//  CityViewController.m
//  MeiTuan
//
//  Created by Ansen on 15-3-9.
//  Copyright (c) 2015年 Ansen. All rights reserved.
//

#import "CityViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "MJExtension.h"
#import "CityGroup.h"
#import "CitySearchResultViewController.h"
#import "UIView+AutoLayout.h"
#import "Const.h"

@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *cityGroups;
@property (nonatomic ,weak) CitySearchResultViewController *citySearhRsult;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 基本设置
    self.title = @"切换城市";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(close) image:@"btn_navigation_close" highImage:@"btn_navigation_close_hl"];
    
//    self.tableView.sectionIndexBackgroundColor = [UIColor redColor];
    self.tableView.sectionIndexColor = [UIColor blackColor];
    
    // 加载城市数据
    _cityGroups = [CityGroup objectArrayWithFilename:@"cityGroups.plist"];
    
    _searchBar.tintColor = Color(32, 191, 179);
}

-(CitySearchResultViewController *)citySearhRsult
{
    
    if (!_citySearhRsult) {
        
        CitySearchResultViewController *vc = [[CitySearchResultViewController alloc] init];
        [self addChildViewController:vc];
        
        self.citySearhRsult = vc;
        
        [self.view addSubview:self.citySearhRsult.view];
        
        [self.citySearhRsult.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
        [self.citySearhRsult.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.searchBar withOffset:15];
    }
    
    return _citySearhRsult;
}

- (void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 搜索框代理方法
/**
 *  键盘弹出:搜索框开始编辑文字
 */
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // 1.隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    // 2.显示搜索框右边的取消按钮
    [searchBar setShowsCancelButton:YES animated:YES];
    
    // 3.修改搜索框的背景图片
    [searchBar setBackgroundImage:[UIImage imageNamed:@"bg_login_textfield_hl"]];
    
    // 4.显示遮盖
    [UIView animateWithDuration:0.5 animations:^{
        self.cover.alpha = 0.5;
    }];
}

/**
 *  键盘退下:搜索框结束编辑文字
 */
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    // 1.显示导航栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    // 2.隐藏搜索框右边的取消按钮
    [searchBar setShowsCancelButton:NO animated:YES];
    
    // 3.修改搜索框的背景图片
    [searchBar setBackgroundImage:[UIImage imageNamed:@"bg_login_textfield"]];
    
    // 4.隐藏遮盖
    [UIView animateWithDuration:0.5 animations:^{
        self.cover.alpha = 0.0;
    }];
    
    self.citySearhRsult.view.hidden = YES;
    searchBar.text = nil;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchBar.text.length) {
        self.citySearhRsult.view.hidden = NO;
        self.citySearhRsult.searchText = searchText;
    }else{
        self.citySearhRsult.view.hidden = YES;
    }
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return _cityGroups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CityGroup *group = _cityGroups[section];
    return group.cities.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"city";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    CityGroup *group = _cityGroups[indexPath.section];
    cell.textLabel.text = group.cities[indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CityGroup *group = _cityGroups[section];
    return group.title;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [_cityGroups valueForKeyPath:@"title"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityGroup *group = _cityGroups[indexPath.section];
    NSString *cityName = group.cities[indexPath.row];
    [NotificationCenter postNotificationName:CityDidChangeNotification object:nil userInfo:@{SelectCityName:cityName}];
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)coverClick:(UIButton *)sender {
    
    [self.searchBar resignFirstResponder];
    
}
@end
